package com.qinggeng.project.api.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import com.qinggeng.common.enums.CommonEnum;
import com.qinggeng.common.enums.OrdersDistStatusEnum;
import com.qinggeng.common.enums.OrdersStatusEnum;
import com.qinggeng.project.system.domain.Orders;
import com.qinggeng.project.system.domain.OrdersGift;
import com.qinggeng.project.system.service.IOrdersService;
import com.qinggeng.project.system.service.impl.OrdersServiceImpl;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import static com.qinggeng.common.enums.OrdersStatusCodeEnum.getOrdersStatusCode;

/**
 * 订单列表
 */
@Data
public class OrdersDto {

    private Long ordersId;
    //订单编号
    private String ordersNo;

    //创建时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    /**
     *  状态码
     *  00=未知
     *  10=待付款，20=待发货，30=待收货，40=待评价，50=已完成，
     *  60=已取消，70=支付超时，80=退单中，90=已退单，100=退单失败, 110=骑手有新的订单
     */
    private String statusCode;
    //状态
    private String status;

    //支付价格
    private BigDecimal payPrice;

    //优惠价格
    private BigDecimal reducePrice;

    //退款金额
    private BigDecimal payment;

    //商户手机号
    private String shopPhone;

    //骑手手机号
    private String courierPhone;

    //支付超时时间
    private Long payOutTime;

    //商品列表
    private List<AddOrdersInfoGoodsDto> goodsList;

    //奖品列表
    private List<OrdersGift> giftList;

    //订单配送方式（10=配送 20=自提）
    private String distType;

    //订单类型   普通订单("10"),免费送("20"),拼团订单("30"),商家补贴("40"),捶一捶("50"),好友助力("60")
    private String ordersType;


    private IOrdersService ordersService;



    public static List<OrdersDto> newsList(List<Orders> ordersList) {
        List<OrdersDto> ordersDtoList = Lists.newArrayList();
        OrdersDto ordersDto = null;
        for (Orders orders : ordersList) {
            ordersDto = new OrdersDto();
            ordersDto.setOrdersId(orders.getOrdersId());
            ordersDto.setOrdersNo(orders.getOrdersNo());
            ordersDto.setStatusCode(getOrdersStatusCode(orders).getCode());
            ordersDto.setStatus(getOrdersStatusCode(orders).getText());
            ordersDto.setCreateTime(orders.getCreateTime());
            ordersDto.setDistType(orders.getDistType());
            ordersDto.setOrdersType(orders.getOrdersType());
            if (orders.getGoodsList()!=null && orders.getGoodsList().size()!=0){
                ordersDto.setGoodsList(AddOrdersInfoGoodsDto.newsList(orders.getGoodsList()));
            }else{
                ordersDto.setGoodsList(Lists.newArrayList());
            }
            if(orders.getGiftList()!=null&&orders.getGiftList().size()>0){
                ordersDto.setGiftList(orders.getGiftList());
            }else{
                ordersDto.setGiftList(Lists.newArrayList());
            }
            ordersDto.setPayPrice(orders.getPayPrice());
            ordersDto.setReducePrice(orders.getReducePrice());
            ordersDto.setPayment(orders.getPayment());
            if (orders.getShop() != null){
                ordersDto.setShopPhone(orders.getShop().getContactPhone());
            }
            if (orders.getCourier() != null){
                ordersDto.setCourierPhone(orders.getCourier().getPhone());
            }
            if (orders.getPayOutTime() != null){
                ordersDto.setPayOutTime(orders.getPayOutTime().getTime());
            }

            ordersDtoList.add(ordersDto);
        }

        return ordersDtoList;
    }
}
